import Vue from 'vue'
import VueRouter from 'vue-router'
import common from '@/common'
// import Login from '@/pages/login/Login'
// import HelloWorld from '@/components/HelloWorld'
import UserList from '@/pages/user/UserList'
import Settings from '../pages/settings/Settings'
import ImageSearchConfigList from '../pages/image-search-config/ImageSearchConfigList'
import MembershipList from '../pages/membership/MembershipList'
import AppsList from '../pages/membership/AppsList'
import ResourceList from '@/pages/resource/ResourceList'
import MResourceList from '@/pages/mresource/ResourceList'
import RoleList from '@/pages/role/RoleList'
import ImageStore from '@/pages/image-store/ImageStore'
import ImageList from '@/pages/image-store/ImageList'
import ImageCategory from '@/pages/image-store/ImageCategory'
import ImageSearch from '@/pages/image-store/ImageSearch'
import ChartFindingRecord from '@/pages/chart-finding/ChartFindingRecord'
import Message from '../pages/message/Message'
import ProductList from '../pages/products/ProductList'
Vue.use(VueRouter)

let router = new VueRouter({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'index',
      component: require('@/pages/Index.vue').default,
      meta: {
        keepAlive: false,
        requiresAuth: true
      },
      children: [
        {
          path: 'message',
          name: 'message',
          components: {
            'rvMessage': Message
          },
          meta: {
            keepAlive: false,
            requiresAuth: true
          }
        },
        {
          path: 'image-search-config',
          name: 'imageSearchConfigList',
          components: {
            'rvImageSearchConfigList': ImageSearchConfigList
          },
          meta: {
            keepAlive: false,
            requiresAuth: true
          }
        },
        {
          path: 'membership',
          name: 'membershipList',
          components: {
            'rvMembershipList': MembershipList
          },
          meta: {
            keepAlive: false,
            requiresAuth: true
          }
        },
        {
          path: 'apps',
          name: 'appsList',
          components: {
            'rvAppsList': AppsList
          },
          meta: {
            keepAlive: false,
            requiresAuth: true
          }
        },
        {
          path: 'products',
          name: 'productsList',
          components: {
            'rvProductsList': ProductList
          },
          meta: {
            keepAlive: false,
            requiresAuth: true
          }
        },
        {
          path: 'image-store',
          name: 'imageStore',
          components: {
            'rvImageStore': ImageStore
          },
          meta: {
            keepAlive: false,
            requiresAuth: true
          }
        },
        {
          path: 'image-list',
          name: 'imageList',
          components: {
            'rvImageStore': ImageList
          },
          meta: {
            keepAlive: false,
            requiresAuth: true
          }
        },
        {
          path: 'image-search',
          name: 'imageSearch',
          components: {
            'rvImageSearch': ImageSearch
          },
          meta: {
            keepAlive: false,
            requiresAuth: true
          }
        },
        {
          path: 'image-category',
          name: 'imageCategory',
          components: {
            'rvImageCategory': ImageCategory
          },
          meta: {
            keepAlive: false,
            requiresAuth: true
          }
        },
        {
          path: 'chart-finding',
          name: 'chartFinding',
          components: {
            'rvChartFindingRecord': ChartFindingRecord
          },
          meta: {
            keepAlive: false,
            requiresAuth: true
          }
        },
        {
          path: 'user-list',
          name: 'userList',
          components: {
            'rvUserList': UserList
          },
          meta: {
            keepAlive: false,
            requiresAuth: true
          }
        },
        {
          path: 'resource-list',
          name: 'resourceList',
          components: {
            'rvResourceList': ResourceList
          },
          meta: {
            keepAlive: false,
            requiresAuth: true
          }
        },
        {
          path: 'mresource-list',
          name: 'mresourceList',
          components: {
            'rvMResourceList': MResourceList
          },
          meta: {
            keepAlive: false,
            requiresAuth: true
          }
        },
        {
          path: 'role-list',
          name: 'roleList',
          components: {
            'rvRoleList': RoleList
          },
          meta: {
            keepAlive: false,
            requiresAuth: true
          }
        },
        {
          path: 'settings',
          name: 'settings',
          components: {
            'rvSettings': Settings
          },
          meta: {
            keepAlive: false,
            requiresAuth: true
          }
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/pages/login/Login'),
      meta: {
        keepAlive: false,
        requiresAuth: false
      }
    }
  ]
})

router.beforeEach((to, form, next) => {
  if (to.meta.requiresAuth && to.meta.requiresAuth === true) {
    let token = common.getToken()
    if (!token) {
      next('/login')
      return
    }
  }
  next()
})

export default router
